//
//  ViewController.swift
//  NavChange
//
//  Created by 郑东喜 on 2017/1/5.
//  Copyright © 2017年 郑东喜. All rights reserved.
//

import UIKit



class ViewController: UITableViewController {


    let halfHeight : CGFloat = round(UIScreen.main.bounds.height) * 0.5 - 64
    
    var vieww = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.navigationItem.title = "百惠"

        self.navigationController?.navigationBar.cnSetBackgroundColor(UIColor.clear)
        
        tableView.backgroundView = UIImageView.init(image: UIImage.init(named: "bg"))
        
        view.backgroundColor = UIColor.white
        
        tableView.contentInset = UIEdgeInsetsMake(halfHeight, 0, 0, 0)

    }
   
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        
        self.navigationController?.navigationBar.barStyle = .black
        
        self.scrollViewDidScroll(self.tableView)
        
        self.navigationController?.navigationBar.shadowImage = UIImage();
    }
    
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let color = UIColor.init(red: 45/255, green: 45/255, blue: 45/255, alpha: 1)
        let offsetY = scrollView.contentOffset.y
        
        
        if offsetY >= -halfHeight - 64 {
            let alpha = [1,(halfHeight + 64 + offsetY) / halfHeight].min()
            self.navigationController?.navigationBar.cnSetBackgroundColor(color.withAlphaComponent(alpha!))
            
            
            
//            vieww.alpha = 1 - alpha!
            
        } else {
            self.navigationController?.navigationBar.cnSetBackgroundColor(color.withAlphaComponent(0))
        }
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = UITableViewCell.init(style: .default, reuseIdentifier: "cellId") as? TableViewCell
        if cell == nil {
            cell = TableViewCell(style: .default, reuseIdentifier: "cellID")
        }
        
        cell?.nameLabel.text = "aaa"

        return cell!
    }
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 30
    }
}

